package com.ruoyi.produce.process.worksheet.service;

import com.ruoyi.DingDing.Domain.DingDingText;
import com.ruoyi.constant.ERPConstant;
import com.ruoyi.common.utils.StringUtils;

import com.ruoyi.personnel.oa.approve.domain.ApproveDomain;
import com.ruoyi.personnel.oa.approve.mapper.ApproveMapper;
import com.ruoyi.personnel.oa.sequence.service.IApproveSequenceService;
import com.ruoyi.produce.process.worksheet.domain.ProduceProcedureTransferSheet;
import com.ruoyi.produce.process.worksheet.domain.ProduceWorkSheet;
import com.ruoyi.produce.process.worksheet.domain.ProduceWorkSheetAllocationRecord;
import com.ruoyi.produce.process.worksheet.domain.WorkSheetProcedure;
import com.ruoyi.produce.process.worksheet.mapper.ProduceWorkSheetMapper;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 加工单
 * 业务执行层
 * @author Drj 2021/6/28
 * 修改：lsy 2021.7.17
 */
@Service
public class ProduceWorkSheetServiceImpl implements IProduceWorkSheetService {
    @Autowired
    private ProduceWorkSheetMapper produceWorkSheetMapper;
    @Autowired
    private IApproveSequenceService approveSequenceService;
    @Autowired
    private ApproveMapper approveMapper;
    @Autowired
    private IdUtil idUtil;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查找加工单
     *
     * @param produceWorkSheet
     * @return
     */
    @Override
    public List<ProduceWorkSheet> selectProduceWorkSheetList(ProduceWorkSheet produceWorkSheet) {
        return setOtherLists(produceWorkSheetMapper.selectProduceWorkSheetList(produceWorkSheet));
    }

    /**
     * 查找正在生产的加工单
     * @param produceWorkSheet
     * @return
     */
    @Override
    public List<ProduceWorkSheet> selectProcessingProduceWorkSheetList(ProduceWorkSheet produceWorkSheet) {
        return setOtherLists(produceWorkSheetMapper.selectProcessingProduceWorkSheetList(produceWorkSheet));
    }

    /**
     * 查找生产完成的加工单
     * @param produceWorkSheet
     * @return
     */
    @Override
    public List<ProduceWorkSheet> selectFinishedProduceWorkSheetList(ProduceWorkSheet produceWorkSheet) {
        return setOtherLists(produceWorkSheetMapper.selectFinishedProduceWorkSheetList(produceWorkSheet));
    }

    public List<ProduceWorkSheet> setOtherLists(List<ProduceWorkSheet> produceWorkSheets){
        for (int i = 0; i < produceWorkSheets.size(); i++) {
            //存入产线分配记录
            produceWorkSheets.get(i).setProduceWorkSheetAllocationRecords(produceWorkSheetMapper.selectProduceWorkSheetAllocationRecordList(new ProduceWorkSheetAllocationRecord(produceWorkSheets.get(i))));
            //存入工序移交记录
            produceWorkSheets.get(i).setProduceProcedureTransferSheets(produceWorkSheetMapper.selectProduceProcedureTransferSheetList(new ProduceProcedureTransferSheet(produceWorkSheets.get(i))));
            //获取对应所需工序
            List<WorkSheetProcedure> workSheetProcedures = produceWorkSheetMapper.selectWorkSheetProcedureList(produceWorkSheets.get(i).getProduceId());
            //获取对应所需工序,存入数据
            produceWorkSheets.get(i).setWorkSheetProcedures(setData(produceWorkSheets.get(i).getWorkSheetNumber(),workSheetProcedures,produceWorkSheets.get(i).getProduceProcedureTransferSheets()));
        }
        return produceWorkSheets;
    }
    public List<WorkSheetProcedure> setData(Double workSheetNumber,List<WorkSheetProcedure> workSheetProcedures,List<ProduceProcedureTransferSheet> produceProcedureTransferSheets){
        for (WorkSheetProcedure workSheetProcedure : workSheetProcedures) {
            BigDecimal bigDecimalCN = new BigDecimal("0");//完成数量 completedNumber
            BigDecimal bigDecimalRN = new BigDecimal("0");//接收数量 receivedNumber
            BigDecimal bigDecimalSN = new BigDecimal("0");//报废数量 scrapNumber
            for (ProduceProcedureTransferSheet produceProcedureTransferSheet : produceProcedureTransferSheets) {
                switch (produceProcedureTransferSheet.getTransferSheetStatus()) {
                    case "1"://完工
                        if (workSheetProcedure.getProcedureId().equals(produceProcedureTransferSheet.getProcedureCurrentId())) {
                            //等于当前工序时，增加完成数量
                            bigDecimalCN = bigDecimalCN.add(new BigDecimal(produceProcedureTransferSheet.getTransferNumber()));
                        }
                        if (workSheetProcedure.getProcedureId().equals(produceProcedureTransferSheet.getProcedureTransferId())) {
                            //等于转到工序时，增加接收数量
                            bigDecimalRN = bigDecimalRN.add(new BigDecimal(produceProcedureTransferSheet.getTransferNumber()));
                        }
                        break;
                    case "2"://返工
                        if (workSheetProcedure.getProcedureId().equals(produceProcedureTransferSheet.getProcedureTransferId())) {
                            //等于转到工序时，减少完成数量
                            bigDecimalCN = bigDecimalCN.subtract(new BigDecimal(produceProcedureTransferSheet.getTransferNumber()));
                        }
                        if (workSheetProcedure.getProcedureId().equals(produceProcedureTransferSheet.getProcedureCurrentId())) {
                            //等于当前工序时，减少接收数量
                            bigDecimalRN = bigDecimalRN.subtract(new BigDecimal(produceProcedureTransferSheet.getTransferNumber()));
                        }
                        break;
                    case "3":
                        //报废，无转到工序
                        //增加报废数量
                        if (workSheetProcedure.getProcedureId().equals(produceProcedureTransferSheet.getProcedureCurrentId())) {
                            //等于当前工序
                            bigDecimalSN = bigDecimalSN.add(new BigDecimal(produceProcedureTransferSheet.getTransferNumber()));
                        }
                        break;
                }
            }
            workSheetProcedure.setIssuedNumber(workSheetNumber);
            workSheetProcedure.setCompletedNumber(bigDecimalCN.doubleValue());
            workSheetProcedure.setReceivedNumber(bigDecimalRN.doubleValue());
            workSheetProcedure.setScrapNumber(bigDecimalSN.doubleValue());
        }
        return workSheetProcedures;
    }

    /**
     * 根据加工单号查找
     *
     * @param workSheetId 加工单号
     * @return
     */
    @Override
    public ProduceWorkSheet selectProduceWorkSheetById(String workSheetId) {

        return produceWorkSheetMapper.selectProduceWorkSheetById(workSheetId);
    }

    /**
     * 新增加工单
     *
     * @param produceWorkSheet
     * @return
     */
    @Override
    public int insertProduceWorkSheet(ProduceWorkSheet produceWorkSheet) throws Exception {
        String workSheetId = idUtil.createWorkSheetId();
        if (StringUtils.isNotNull(produceWorkSheetMapper.selectProduceWorkSheetById(workSheetId))){
            //存在
            return 0;
        }
        produceWorkSheet.setWorkSheetId(workSheetId);
        ApproveDomain approve = new ApproveDomain(workSheetId, "加工单审批", ERPConstant.APPROVAL_TYPE_WORKSHEET, produceWorkSheet.getCreateById()); //新增审批
        approve.setCurrentApprover(approveSequenceService.getLowestRoleId(ERPConstant.APPROVAL_TYPE_WORKSHEET));
        approveMapper.insertApprove(approve);
        Long currentApprover = approve.getCurrentApprover();
        DingDingText dingDingText = new DingDingText();
        dingDingText.BatchRobotText(currentApprover);
        return produceWorkSheetMapper.insertProduceWorkSheet(produceWorkSheet);
    }

    /**
     * 更新加工单
     *
     * @param produceWorkSheet
     * @return
     */
    @Override
    public int updateProduceWorkSheet(ProduceWorkSheet produceWorkSheet) {
        if (!StringUtils.isNotNull(produceWorkSheetMapper.selectProduceWorkSheetById(produceWorkSheet.getWorkSheetId()))){
            //不存在
            return 0;
        }

        return produceWorkSheetMapper.updateProduceWorkSheet(produceWorkSheet);
    }

    /**
     * 删除加工单
     * 仅修改加工单删除标志
     *
     * @param produceWorkSheet
     * @return
     */
    @Override
    public int updateProduceWorkSheetDeleteFlag(ProduceWorkSheet produceWorkSheet) {
        return produceWorkSheetMapper.updateProduceWorkSheetDeleteFlag(produceWorkSheet);
    }

    /**
     * 加工单产线分配
     *
     * @param produceWorkSheet
     * @return
     */
    @Override
    public int updateProduceWorkSheetAllocation(ProduceWorkSheet produceWorkSheet) {
        //产线分配
        produceWorkSheetMapper.updateProduceWorkSheetAllocation(produceWorkSheet);
        //产线分配记录
        ProduceWorkSheetAllocationRecord produceWorkSheetAllocationRecord = new ProduceWorkSheetAllocationRecord();
        produceWorkSheetAllocationRecord.setWorkSheetId(produceWorkSheet.getWorkSheetId());//加工单号
        produceWorkSheetAllocationRecord.setProductionLineId(produceWorkSheet.getProductionLineId());//产线编号
        produceWorkSheetAllocationRecord.setAllocationStatus("1");//状态：分配
        produceWorkSheetMapper.insertProduceWorkSheetAllocationRecord(produceWorkSheetAllocationRecord);
        return 1;
    }

    /**
     * 加工单产线分配移除
     *
     * @param produceWorkSheet
     * @return
     */
    @Override
    public int removeProduceWorkSheetAllocation(ProduceWorkSheet produceWorkSheet) {
        ProduceWorkSheetAllocationRecord produceWorkSheetAllocationRecord = new ProduceWorkSheetAllocationRecord();
        produceWorkSheetAllocationRecord.setWorkSheetId(produceWorkSheet.getWorkSheetId());//加工单号
        //获取原有分配产线编号
        produceWorkSheetAllocationRecord.setProductionLineId(produceWorkSheetMapper.selectProduceWorkSheetById(produceWorkSheet.getWorkSheetId()).getProductionLineId());
        produceWorkSheetAllocationRecord.setAllocationStatus("2");//状态：移除
        //产线分配移除
        produceWorkSheetMapper.removeProduceWorkSheetAllocation(produceWorkSheet);
        //产线分配移除记录
        produceWorkSheetMapper.insertProduceWorkSheetAllocationRecord(produceWorkSheetAllocationRecord);
        return 1;
    }

    /**
     * 查找加工单产线分配记录
     *
     * @param produceWorkSheetAllocationRecord
     * @return
     */
    @Override
    public List<ProduceWorkSheetAllocationRecord> selectProduceWorkSheetAllocationRecordList(ProduceWorkSheetAllocationRecord produceWorkSheetAllocationRecord) {
        return produceWorkSheetMapper.selectProduceWorkSheetAllocationRecordList(produceWorkSheetAllocationRecord);
    }

    /**
     * 查找加工单工序移交记录
     *
     * @param produceProcedureTransferSheet
     * @return
     */
    @Override
    public List<ProduceProcedureTransferSheet> selectProduceProcedureTransferSheetList(ProduceProcedureTransferSheet produceProcedureTransferSheet) {
        return produceWorkSheetMapper.selectProduceProcedureTransferSheetList(produceProcedureTransferSheet);
    }

    /**
     * 新增/更新加工单工序移交记录
     *
     * @param produceProcedureTransferSheet
     * @return
     */
    @Override
    public int editProduceProcedureTransferSheet(ProduceProcedureTransferSheet produceProcedureTransferSheet) {
        if (StringUtils.isNotNull(produceWorkSheetMapper.selectProduceProcedureTransferSheetById(produceProcedureTransferSheet.getWorkSheetId(),produceProcedureTransferSheet.getIncreasingId()))){
            //存在
            return produceWorkSheetMapper.updateProduceProcedureTransferSheet(produceProcedureTransferSheet);
        }else{
            //不存在
            return produceWorkSheetMapper.insertProduceProcedureTransferSheet(produceProcedureTransferSheet);
        }
    }

    /**
     * 确认工序移交记录
     *
     * @param produceProcedureTransferSheet
     * @return
     */
    @Override
    public int confirmProduceProcedureTransferSheet(ProduceProcedureTransferSheet produceProcedureTransferSheet) {
        if (StringUtils.isNull(produceWorkSheetMapper.selectProduceProcedureTransferSheetById(produceProcedureTransferSheet.getWorkSheetId(),produceProcedureTransferSheet.getIncreasingId()))) {
            //不存在
            return 0;
        }
        return produceWorkSheetMapper.confirmProduceProcedureTransferSheet(produceProcedureTransferSheet);
    }


}
